package org.gntc.prayer.myprayer;

import org.gntc.prayer.R;
import org.gntc.prayer.common.PrayerInfo;
import org.gntc.prayer.db.PrayerDBMngr;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PrayerViewActivity extends Activity
{
	/** Called when the activity is first created. */
	
	private int m_nPrayerID;
	private PrayerDBMngr m_PrayerDBMngr;
	
	/**
	 * 삭제 시  button click listener
	 */
	private DialogInterface.OnClickListener m_onClickListener_btnYes = new DialogInterface.OnClickListener()
	{
		public void onClick(DialogInterface arg0, int arg1)
		{
			// Delete Prayer
			m_PrayerDBMngr.deletePrayer(m_nPrayerID);
			Toast.makeText(PrayerViewActivity.this, R.string.prayer_deleted, Toast.LENGTH_SHORT).show();
			finish();
		}
	};
	
	/**
	 * Add answer button click listener
	 */
	private View.OnClickListener m_onClickListener_addAnswer = new Button.OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(PrayerViewActivity.this, AnswerEditActivity.class);
			intent.putExtra("prayer_id", m_nPrayerID);
			startActivity(intent);
		}
	};
	
	/**
	 * onCreate
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.prayer_view);
	    
	    // Add answer button click listener
	    Button btnAddAnswer = (Button)findViewById(R.id.btn_add_answer);
	    btnAddAnswer.setOnClickListener(m_onClickListener_addAnswer);
	    
	    // 리스트에서 넘겨 받은 Prayer ID 구하기
	    Intent intent = getIntent();
	    Bundle extra = intent.getExtras();
	    
	    if (extra != null)
	    {
	    	m_nPrayerID = (int)extra.getLong("id");
	    }
	    else
	    {
	    	// Prayer ID가 없으므로 Activity 종료
	    	Toast.makeText(PrayerViewActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
	    	finish();
	    }
	    
	    // DB Open
	    initDB();
	}
	
	/**
	 * onResume
	 */
	@Override
	public void onResume()
	{
		super.onResume();
		
		// Prayer 정보 가져오기
	    getPrayerInfo();
	}
	
	/**
	 * Menu
	 */
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	getMenuInflater().inflate(R.menu.prayer_view_menu, menu);
    	return true;
    }
	
	/**
	 * Menu selected
	 */
	@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch( item.getItemId() )
    	{
    	case R.id.prayer_view_btn_edit :
    		Intent intent = new Intent(PrayerViewActivity.this, PrayerEditActivity.class);
			intent.putExtra("edit_mode", true);
			intent.putExtra("id", m_nPrayerID);
			startActivity(intent);
    		break;
    		
    	case R.id.prayer_view_btn_del :
    		// Yes, No 팝업 창
			AlertDialog.Builder alt_bld = new AlertDialog.Builder(PrayerViewActivity.this);
			
			alt_bld
			.setTitle(R.string.delete_prayer)
			.setMessage(R.string.delete_prayer_q).setCancelable(false)
			.setPositiveButton(R.string.yes, m_onClickListener_btnYes)
	        .setNegativeButton(R.string.no, null);
			
			AlertDialog altDlg = alt_bld.create();
			altDlg.show();
    		break;
    		
    	default :
    		return false;
    	}
    	
    	return true;
    }
	
	/**
	 * DB 초기화
	 */
	private void initDB()
	{
		m_PrayerDBMngr = new PrayerDBMngr(PrayerViewActivity.this);
		
		try
		{
			m_PrayerDBMngr.open();
		}
		catch (SQLiteException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * onDestroy
	 */
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		
		// DB close
		m_PrayerDBMngr.close();
	}
	
	/**
	 * Prayer 정보 가져오기
	 */
	private void getPrayerInfo()
	{
		PrayerInfo pi = m_PrayerDBMngr.getPrayerInfo(m_nPrayerID);
		
		// Category name
		TextView tvCateName = (TextView) findViewById(R.id.view_catename);
		tvCateName.setText(pi.getM_strCateName());
		
		// Prayer
		TextView tvPrayer = (TextView) findViewById(R.id.view_prayer);
		tvPrayer.setText(pi.getM_strPrayer());
		
		// Registration date
		TextView tvRegDate = (TextView) findViewById(R.id.view_reg_date);
		tvRegDate.setText(pi.getM_strRegDate());
		
		// Alarm
		TextView tvAlarmTime = (TextView) findViewById(R.id.view_alarm);
		
		if (pi.getM_bIsSetAlarm())
		{
			tvAlarmTime.setText(pi.getM_strAlarmTime());
		}
		else
		{
			tvAlarmTime.setText(getString(R.string.not_set));
		}
		
		// Answer list
		getAnswerList();
	}
	
	/**
	 * Get answer list
	 */
	public void getAnswerList()
	{
		LinearLayout answerList = (LinearLayout) findViewById(R.id.answer_list);
		answerList.removeAllViews();
		
		Cursor c = m_PrayerDBMngr.getAllAnswerList(m_nPrayerID);
		c.moveToFirst();
		
		int nTotalCnt = c.getCount();
		
		if (nTotalCnt > 0)
		{
			// Answer header
			LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		    View answerHeaderView = li.inflate(R.layout.answer_list_header, null);
		    answerList.addView(answerHeaderView);
		    
		    for (int i = 0; i < nTotalCnt; i++)
		    {
		    	final int nAnswerID = c.getInt(0);
		    	View answerItemView = li.inflate(R.layout.answer_list_item, null);
		    	
		    	// Answer
		    	TextView tvAnswer = (TextView) answerItemView.findViewById(R.id.view_answer);
		    	tvAnswer.setText(c.getString(1));
			    
			    // Registration date
			    TextView tvRegDate = (TextView) answerItemView.findViewById(R.id.view_answer_regdate);
			    tvRegDate.setText(c.getString(2));
			    
			    
			    // Edit button
			    Button btnAnswerEdit = (Button) answerItemView.findViewById(R.id.view_btn_answer_edit);
			    
			    btnAnswerEdit.setOnClickListener
			    (
		        	new OnClickListener()
		        	{
		        		public void onClick(View v)
		        		{
		        			Intent intent = new Intent(PrayerViewActivity.this, AnswerEditActivity.class);
		        			intent.putExtra("edit_mode", true);
		        			intent.putExtra("prayer_id", m_nPrayerID);
		        			intent.putExtra("answer_id", nAnswerID);
		        			startActivity(intent);		        			
		        		}
		        	}
		        );
			    
			    // Delete button
			    Button btnAnswerDel = (Button) answerItemView.findViewById(R.id.view_btn_answer_del);
			    
			    btnAnswerDel.setOnClickListener
			    (
		        	new OnClickListener()
		        	{
		        		public void onClick(View v)
		        		{
		        			DialogInterface.OnClickListener onClickListener_btnYes = new DialogInterface.OnClickListener()
		        			{
		        				public void onClick(DialogInterface arg0, int arg1)
		        				{
		        					// Delete answer
		        					m_PrayerDBMngr.deleteAnswer(nAnswerID);
		        					Toast.makeText(PrayerViewActivity.this, R.string.answer_deleted, Toast.LENGTH_SHORT).show();
		        					getPrayerInfo();
		        				}
		        			};
		        			
		        			// Yes, No 팝업 창
		        			AlertDialog.Builder alt_bld = new AlertDialog.Builder(PrayerViewActivity.this);
		        			
		        			alt_bld
		        			.setTitle(R.string.delete_answer)
		        			.setMessage(R.string.delete_answer_q).setCancelable(false)
		        			.setPositiveButton(R.string.yes, onClickListener_btnYes)
		        	        .setNegativeButton(R.string.no, null);
		        			
		        			AlertDialog altDlg = alt_bld.create();
		        			altDlg.show();
		        		}
		        	}
		        );
			    
			    // Add view
			    answerList.addView(answerItemView);
			    
			    // Next data
			    c.moveToNext();
		    }
		    
		    c.close();
		}
	}

}
